package com.btpj.hospital_server.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 通用封装分页对象
 *
 * @author BTPJ  2020/8/24
 */
@Data
@ApiModel("通用封装分页对象")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonPage<T> implements Serializable {

    @ApiModelProperty("第几页")
    private Integer pageNo;

    @ApiModelProperty("每页大小")
    private Integer pageSize;

    @ApiModelProperty("总页数")
    private Integer totalPage;

    @ApiModelProperty("结果总数")
    private Long totalSize;

    @ApiModelProperty("结果列表")
    private List<T> list;

    /**
     * 使用PageHelper将list转为分页信息
     *
     * @param list 数据集
     * @param <T>  分页后的数据集
     * @return list转后的分页信息
     */
    public static <T> CommonPage<T> listPage(List<T> list) {
        CommonPage<T> result = new CommonPage<>();
        PageInfo<T> pageInfo = new PageInfo<>(list);
        result.setTotalSize(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        result.setPageNo(pageInfo.getPageNum());
        result.setTotalPage(pageInfo.getPages());
        result.setPageSize(pageInfo.getPageSize());
        return result;
    }
}
